﻿#include "sysprotocoleditor.h"
#include "ui_sysprotocoleditor.h"
#include "probutility.h"

SysProtocolEditor::SysProtocolEditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SysProtocolEditor)
{
    ui->setupUi(this);
    nWindType = ADD_DATA;
    nOldSysId = -1;
}

SysProtocolEditor::~SysProtocolEditor()
{
    delete ui;
}

void SysProtocolEditor::InitSysProtocolEditor(int nType,int nId,const QString sInfo)
{
    ui->InputEdit->clear();
    ui->NameEdit->clear();

    QString sTitleName = "";
    QString sIcoPaht = "";
    nWindType = nType;
    nOldSysId = nId;
    switch (nType)
    {
    case ADD_DATA:
    {
        sTitleName = "添加系统";
        sIcoPaht =":/Icon/Add.png";
        break;
    }
    case EDIT_DATA:
    {
        sTitleName = "编辑系统";
        sIcoPaht = ":/Icon/Edit.png";
        ui->InputEdit->setText(QString::number(nId));
        ui->NameEdit->setText(sInfo);
        break;
    }
    }
    setWindowIcon(QIcon(sIcoPaht));
    setWindowTitle(sTitleName);
    ui->InputEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
    setFixedSize(400,300);
}


void SysProtocolEditor::on_EenterBtn_clicked()
{
    int nSysId = ui->InputEdit->text().toInt();
    QString sInfo = ui->NameEdit->text();
    if(nSysId < 0)
    {
        QMessageBox::critical(this, "错误", "Id不能小于0!","确定");
        return;
    }

    if(sInfo.isEmpty())
    {
        QMessageBox::critical(this, "错误", "数据描述不能为空!","确定");
        return;
    }

    close();
    if(nWindType == ADD_DATA)       //如果是新建系统，检查当前是否已经存在，存在则创建失败
    {
        emit on_CreateSysProtocol(nSysId,sInfo);
    }else                                                       //如果是编辑系统，检查当前是否已经存在，存在则修改，不存在则创建
    {
        emit on_EditorSysProtocol(nSysId,sInfo,this->nOldSysId);
    }
}

void SysProtocolEditor::on_QuitBtn_clicked()
{
    ui->InputEdit->clear();
    ui->NameEdit->clear();
    close();
}

